package com.dbumama.market.web.core.utils;

import com.jfinal.kit.StrKit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具
 *
 * @author Javen
 *         2016年4月2日
 */

public class RegexUtils {

    /**
     * 用户名
     */
    public static final String USER_NAME = "^[a-zA-Z\\u4E00-\\u9FA5][a-zA-Z0-9_\\u4E00-\\u9FA5]{1,11}$";

    /**
     * 密码
     */
    public static final String USER_PASSWORD = "^[A-Za-z0-9]{6,20}$";
    
    /**
     * 支付密码
     */
    public static final String ZF_PASSWORD = "^[0-9]{6}$";

    /**
     * 邮箱
     */
    public static final String EMAIL = "^\\w+([-+.]*\\w+)*@([\\da-z](-[\\da-z])?)+(\\.{1,2}[a-z]+)+$";

    /**
     * 手机号
     */
    public static final String PHONE = "^1[34578]\\d{9}$";

    /**
     * 银行卡号
     */
    public static final String TRANSCARD = "^\\d{16}|\\d{19}$";

    /**
     * 手机号或者邮箱
     */
    public static final String EMAIL_OR_PHONE = EMAIL + "|" + PHONE;

    /**
     * URL路径
     */
    public static final String URL = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})(:[\\d]+)?([\\/\\w\\.-]*)*\\/?$";

    /**
     * 身份证校验，初级校验，具体规则有一套算法
     */
    public static final String ID_CARD = "^\\d{15}$|^\\d{17}([0-9]|X)$";

    /**
     * 编译传入正则表达式和字符串去匹配,忽略大小写
     *
     * @param regex
     * @param beTestString
     * @return
     */
    public static boolean match(String regex, String beTestString) {
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(beTestString);
        return matcher.matches();
    }

    /**
     * 编译传入正则表达式在字符串中寻找，如果匹配到则为true
     *
     * @param regex
     * @param beTestString
     * @return
     */
    public static boolean find(String regex, String beTestString) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(beTestString);
        return matcher.find();
    }

    /**
     * 编译传入正则表达式在字符串中寻找，如果找到返回第一个结果<br/>
     * 找不到返回null
     *
     * @param regex
     * @param beFoundString
     * @return
     */
    public static String findResult(String regex, String beFoundString) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(beFoundString);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    /**
     * 隐藏手机号中间4位
     *
     * @param phone
     * @return String
     */
    public static String encodePhone(String phone) {
        if (StrKit.isBlank(phone)) {
            return "";
        }
        if (match(PHONE, phone)) {
            String begin = phone.substring(0, 3);
            String end = phone.substring(7, phone.length());
            return begin + "****" + end;
        }
        return phone;
    }
    
    /**
     * 隐藏身份证中间11位
     * @param idCard
     * @return
     */
    public static String encodeIdCard(String idCard){
    	
    	if (StrKit.isBlank(idCard)) {
            return "";
        }
    	
    	String regex = "(\\w{3})(\\w+)(\\w{4})";
        
    	idCard = idCard.replaceAll(regex, "$1***********$3"); 
    	
    	return idCard;
    }
    
    /**
     * 隐藏银行卡中间11位
     * @param bankCard
     * @return
     */
    public static String encodeBankCard(String bankCard){
    	
    	if (StrKit.isBlank(bankCard)) {
            return "";
        }
    	
    	String regex = "(\\w{4})(\\w{2})(\\w+)(\\w{4})";
        
    	bankCard = bankCard.replaceAll(regex, "$1 $2** **** $4"); 
    	
    	return bankCard;
    }
    
    /**
     * 截取银行卡后4位
     * @param bankCard
     * @return
     */
    public static String splitBankCard(String bankCard){
    	
    	if (StrKit.isBlank(bankCard)) {
            return "";
        }
    	String newBankCard = bankCard.substring(bankCard.length()-4, bankCard.length());
    	
    	return newBankCard;
    }
    
    /**
     * 隐藏姓名中间1位
     * @param strName
     * @return
     */
    public static String encodeName(String strName){
    	
    	try{
    	
	    	if (StrKit.isBlank(strName)) {
	            return "";
	        }
	    	if(strName.length()<2){
	    		return "";
	    	}
	
	    	//保留姓氏
	    	char str2=strName.charAt(0);
	    	
	    	char strend = ' ';
	
	    	//截取名字
	    	String str1=strName.substring(1);
	    	
	    	if(strName.length()>2){
	    		strend = strName.charAt(strName.length()-1);
	    		str1 = str1.substring(0, str1.length()-1);
	    	}
	
	    	//用正则表达式替换(包括汉字，数字，大小写字母)
	    	str1=str1.replaceAll("[^x00-xff]|\\w", "*");
	
	    	return str2+str1+strend;
    	}catch (Exception e) {
    		// TODO: handle exception
    	}
    	
    	return "";
    }
    
    
    
    public static void main(String[] args) {

    	

    }

}